package xyz.icxl.android.architecture.dialog

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.FragmentManager
import com.airbnb.lottie.LottieAnimationView
import xyz.icxl.android.architecture.R
import xyz.icxl.android.architecture.ui.BaseDialogFragment

class LoadingDialog(private val msg:CharSequence,private val dialogCancelAble:Boolean = true) : BaseDialogFragment() {
    override fun layoutId(): Int = R.layout.dialog_loading
    private lateinit var textView: TextView
    private lateinit var mLottieAnimationView: LottieAnimationView
    override fun doSomething(rootView: View) {
        textView = rootView.findViewById(R.id.dialog_tv)
        isCancelable = dialogCancelAble
        textView.text = msg
        mLottieAnimationView = rootView.findViewById(R.id.progress_lav)
    }

    fun changeText(msg: CharSequence) {
        if (this::textView.isInitialized) {
            textView.text = msg
        }
    }

    fun dismissWait1Second() {

            if (this::textView.isInitialized){
                textView.postDelayed({
                    if (fragmentManager != null){
                        super.dismissAllowingStateLoss()
                    }
                },1200)
            }else{
                super.dismissAllowingStateLoss()
            }

    }


    fun dismissBySuccess(){
        mLottieAnimationView.repeatCount = 0
        mLottieAnimationView.setAnimation(R.raw.dialog_meta_load_success)
        mLottieAnimationView.playAnimation()
        dismissWait1Second()
    }

    fun dismissByFail(){
        mLottieAnimationView.repeatCount = 0
        mLottieAnimationView.setAnimation(R.raw.dialog_meta_load_fail)
        mLottieAnimationView.playAnimation()
        dismissWait1Second()
    }

    fun show(manager: FragmentManager) {
        super.show(manager, "LoadingDialog")
    }

}